package com.c.ctgproject.adapter.guang

import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseEmptyRecyclerViewAdapter
import com.c.ctgproject.base.BaseEmptyRecyclerViewAdapterV2
import com.c.ctgproject.base.BaseRecyclerViewAdapter
import com.c.ctgproject.entity.guang.CarPartHandlerEntity
import com.c.ctgproject.entity.guang.CarPartHandlerEntityV2
import com.c.ctgproject.utils.handlerNull

class CarPartHandlerAdapter(private val context: Context, private val mList: ArrayList<CarPartHandlerEntityV2.DataBean>, private val listener:CarPartHandlerAdapterListener) : BaseRecyclerViewAdapter(context, mList), CarPartHandlerChildAdapter.CarPartHandlerChildAdapterListener {
    override fun createViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_car_part_handler_list, parent, false)
        return MyViewHolder(view)
    }

    override fun handlerData(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyViewHolder) {
            val data = mList[position]
            holder.mCarEnergyET.isEnabled=data.isEnabled
            holder.mCarEnergyET.setText(data.carEngine.handlerNull())
            if(data.carType==1||data.carType==2){

                holder.mCarEnergyLL.visibility=View.VISIBLE
            }else{
                holder.mCarEnergyLL.visibility=View.GONE
            }
            "车辆自编码:${data.carCode.handlerNull()}".also { holder.mNameTV.text = it }
            data.partsList?.let {
                holder.mRecyclerView.adapter = CarPartHandlerChildAdapter(position,context, it,this)
                holder.mRecyclerView.layoutManager = LinearLayoutManager(context)
            }
            val watcher = object : TextWatcher {
                override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

                }

                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

                }

                override fun afterTextChanged(s: Editable?) {
                    if (holder.mCarEnergyET.hasFocus()) {
                        //判断当前EditText是否有焦点在
                        //listener.onNumChange(s.toString(), parentPosition, position)
                        listener.onCarEnergyTextChange(holder.mCarEnergyET.text.toString(),position)
                        //mList[position].carEngine=holder.mCarEnergyET.text.toString()
                    }
                }
            }
            holder.mCarEnergyET.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
                if (hasFocus) {
                    holder.mCarEnergyET.addTextChangedListener(watcher)
                } else {
                    holder.mCarEnergyET.removeTextChangedListener(watcher)
                }
            }

        }
    }

    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val mNameTV: TextView = view.findViewById(R.id.mNameTV)
        val mRecyclerView: RecyclerView = view.findViewById(R.id.mRecyclerView)
        val mCarEnergyLL:LinearLayout=view.findViewById(R.id.mCarEnergyLL)
        val mCarEnergyET:EditText=view.findViewById(R.id.mCarEnergyET)
    }

    override fun onNumChange(str: String, parentPosition: Int, position: Int) {
        listener.onNumChange(str, parentPosition, position)
    }
    interface CarPartHandlerAdapterListener {
        fun onNumChange(str: String, parentPosition: Int, position: Int)
        fun onCarEnergyTextChange(str: String,position: Int)
    }

}